﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Data.SqlClient;
using WFDataBaseProject.Helpers;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.ComponentModel;
using System.Activities.Presentation.Metadata;
using System.Activities.Presentation.PropertyEditing;

namespace WFDataBaseProject
{
   
    [Designer(typeof(UCSynchronizeActivityDesigner))]
    public class SynchronizeActivity :CodeActivity
    {

        public SynchronizeActivity()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();
            builder.AddCustomAttributes(typeof(SynchronizeActivity), "SourceConnectionString",
                    new EditorAttribute(typeof(ConnectionStringEditor),
                    typeof(DialogPropertyValueEditor)));
            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
        
        #region Arguments
      
        [Category("Inputs")]
        [Description("The connection string to connect to the data base")]
        public InArgument<string> SourceConnectionString { get; set; }
        [Category("Inputs")]
        [Description("The source table that is used to synchronize the destination table")]
        public InArgument<string> SourceTable { get; set; }
        [Category("Inputs")]
        [Description("The destination table going to be synchronized")]
        public InArgument<string> DestinationTable { get; set; }


        #endregion

        #region private fields

        string sourceconnectionstring;
        string sourceTable;
        string destinationTable;

        #endregion
       


        protected override void Execute(CodeActivityContext context)
        {   
            sourceconnectionstring = SourceConnectionString.Get<string>(context);
            sourceTable = SourceTable.Get<string>(context);
            destinationTable = DestinationTable.Get<string>(context);

            try
            {
                if (!Validate(sourceTable))
                {
                    throw new FormatException("The source table name should be the full canonical name");
                }
                if (!Validate(destinationTable))
                {
                    throw new FormatException("The destination table name should be the full canonical name");
                }

                SqlCommand command;
                using (SqlConnection sourceconnection = new SqlConnection(sourceconnectionstring))
                {
                    try
                    {
                        sourceconnection.Open();
                        command = new SqlCommand();
                        command.CommandText = string.Format(WFResources.ResourceManager.GetString("mergecommand"),destinationTable,sourceTable);
                        
                        command.Connection = sourceconnection;
                        command.ExecuteNonQuery();
                    }

                    catch (SqlException caught)
                    {
#if DEBUG
                        Debug.Print(caught.Message);
#endif
                    }
                    finally
                    {
                        sourceconnection.Close();
                    }

                }
            }
            catch (FormatException caught)
            {
#if DEBUG
                            Debug.Print(caught.Message);
#endif
            }        

        }

        //^(([a-zA-Z0-9_]+)|(\[[a-zA-Z0-9_\.]+\]))(\.(([a-zA-Z0-9_]+)|(\[[a-zA-Z0-9_\.]+\]))){2}$
        public bool Validate(string stringInput)
        {
            bool result = true;

            if (!Regex.IsMatch(stringInput, @"^(([a-zA-Z0-9_]+)|(\[[a-zA-Z0-9_\.]+\]))(\.(([a-zA-Z0-9_]+)|(\[[a-zA-Z0-9_\.]+\]))){2}$"))
            {
                result = false;
            }
            return result;
        }
    }
}
